package com.dyz.leetcode._06_binarySearch;

public class _34_searchRange {
    public int[] searchRange(int[] nums, int target) {
        int[] res = new int[]{-1, -1};
        if(nums==null || nums.length==0){return res;}
        int left = 0;
        int right = nums.length-1;
        while(left<=right){
            int mid = left+(right-left)/2;
            if(nums[mid]==target){
                int l = mid;
                int r = mid;
                while(l>=0){
                    if(nums[l]!=target){
                        break;
                    }else{
                        res[0] = l;
                        l--;
                    }
                }
                while(r<=right){
                    if(nums[r]==target){
                        res[1] = r;
                        r++;
                    }else{
                        break;
                    }
                }
                break;
            }else if(nums[mid]>target){
                right = mid -1;
            }else{
                left = mid +1 ;
            }
        }
        return res;
    }

    public static void main(String[] args) {
        int[] nums = new int []{5,7,7,8,8,10};
        int[] ints = new _34_searchRange().searchRange(nums, 8);
    }
}
